package app.model;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/** 
 * 
 * @author Nachman Mathieu
 * @author David Gianorio
 * @author Faton Alia
 * 
 * This class will be used to store all applications uploaded by members on the website */

@Entity
@Table(name="APPLICATION")
public class Application {

	@Id
	@GeneratedValue
	private Long id;
	
	private String appName;
	private String appUrl;
	private Date appUploadDate;
	private String appDescription;
	private int appNumberOfDownload;
	private int appRating;
	
	// Relation used to references the creator of an application
	@ManyToOne
	@JoinColumn(name="APP_CREATOR_ID")
	private Member applicationCreator;

	// Relation used to references users who download applications
	@ManyToMany(mappedBy="downloadedApplications")
	private Set<Member> applicationDownloaders = new HashSet<Member>();
	
	// Constructors
	public Application(String appName, String appUrl, Date appUploadDate,
			String appDescription, int appNumberOfDownload, int appRating, Member applicationCreator) {
		super();
		this.appName = appName;
		this.appUrl = appUrl;
		this.appUploadDate = appUploadDate;
		this.appDescription = appDescription;
		this.appNumberOfDownload = appNumberOfDownload;
		this.appRating = appRating;
		this.applicationCreator = applicationCreator;
	}

	public Application(){}

	//////////////////////////////////////////////////////////////////////
	/////////////////////// Generated Code ///////////////////////////////
	//////////////////////////////////////////////////////////////////////

	// Getters
	public String getAppName() {
		return appName;
	}

	public String getAppUrl() {
		return appUrl;
	}

	public Date getAppUploadDate() {
		return appUploadDate;
	}

	public String getAppDescription() {
		return appDescription;
	}

	public int getAppNumberOfDownload() {
		return appNumberOfDownload;
	}

	public int getAppRating() {
		return appRating;
	}
	
	// Setters
	public void setAppName(String appName) {
		this.appName = appName;
	}

	public void setAppUrl(String appUrl) {
		this.appUrl = appUrl;
	}

	public void setAppUploadDate(Date appUploadDate) {
		this.appUploadDate = appUploadDate;
	}

	public void setAppDescription(String appDescription) {
		this.appDescription = appDescription;
	}

	public void setAppNumberOfDownload(int appNumberOfDownload) {
		this.appNumberOfDownload = appNumberOfDownload;
	}

	public void setAppRating(int appRating) {
		this.appRating = appRating;
	}

	public Member getApplicationCreator() {
		return applicationCreator;
	}

	public void setApplicationCreator(Member applicationCreator) {
		this.applicationCreator = applicationCreator;
	}	
	
	//HashCode and Equals
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Application other = (Application) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
}